//
//  ByViewShopsIndexHeader.m
//  chequanbao_ios
//
//  Created by mc on 2017/10/10.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewShopsIndexHeader.h"
#import "ByViewLineArow.h"

@implementation ByViewShopsIndexHeader
{
    NSArray   *_titles;
    NSArray   *_imgs;
    NSArray   *_unImgs;
    NSInteger  _preTag;
    ByViewLineArow *_lineArow;
}

- (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles andImgs:(NSArray *)imgs andUnImgs:(NSArray *)unImgs{
    if (self = [super initWithFrame:frame]) {
        _titles = [titles copy];
        _imgs = [imgs copy];
        _unImgs = [unImgs copy];
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    CGFloat width = [@"休闲娱乐" getSizeOfString:14].width + 5;
    for (int i = 0;i < _titles.count;i ++ ) {
        width = MAX(width, 50);
        CGFloat x = (SCREEN_WIDTH - _titles.count * width) / 5 * (i + 1) + i * width;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 10 + i;
        button.frame = CGRectMake(x, 20, width, 50 + 10 + 20);
        [button setImage:[UIImage imageNamed:_unImgs[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:_imgs[i]] forState:UIControlStateSelected];
        CGSize img_size = button.imageView.frame.size;
        CGSize title_size = button.titleLabel.frame.size;
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:_titles[i] forState:UIControlStateNormal];
        CGFloat margin = (width - 50) / 2;
        button.titleEdgeInsets = UIEdgeInsetsMake(img_size.height + 10, -img_size.width, 0, 0);
        button.imageEdgeInsets = UIEdgeInsetsMake(- title_size.height - 10, margin, 0, - margin);

        button.titleLabel.font = FONT(14);
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
        [self addSubview:button];
        [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    // 设置默认的
    _preTag = 10;
    UIButton *preButton = [self viewWithTag:_preTag];
    preButton.selected = YES;

    CGFloat point_margin = (SCREEN_WIDTH - width * 4) / 5;
    CGFloat point_x = point_margin + (width / 2 - 10);
    CGPoint p2 = CGPointMake(point_x, 10);
    CGPoint p3 = CGPointMake(point_x + 10, 0);
    CGPoint p4 = CGPointMake(point_x + 20, 10);
    [self createLineArow:p2 andPoint:p3 andPoint:p4];

}

- (void)createLineArow:(CGPoint)p2 andPoint:(CGPoint)p3 andPoint:(CGPoint)p4{
    [_lineArow removeFromSuperview];
    _lineArow = [[ByViewLineArow alloc] initWithFrame:CGRectMake(0, 100 + 8, SCREEN_WIDTH, 12) Point2:p2 Point3:p3 Point4:p4];
    [self addSubview:_lineArow];
}

- (void)click:(UIButton *) button{
    if (button.tag == _preTag) {
        return;
    }
    CGFloat width = [@"休闲娱乐" getSizeOfString:14].width + 5;
    CGFloat point_margin = (SCREEN_WIDTH - width * 4) / 5;
    CGFloat point_x = (button.tag - 10 + 1) * point_margin + (button.tag - 10) * width + (width / 2 - 10);
    CGPoint p2 = CGPointMake(point_x, 10);
    CGPoint p3 = CGPointMake(point_x + 10, 0);
    CGPoint p4 = CGPointMake(point_x + 20, 10);
    [self createLineArow:p2 andPoint:p3 andPoint:p4];
    UIButton *preButton = [self viewWithTag:_preTag];
    preButton.selected = NO;
    
    UIButton *selectButton = [self viewWithTag:button.tag];
    selectButton.selected = YES;
    _preTag = button.tag;
    
    if (self.selectBlock) {
        self.selectBlock(_preTag);
    }
}

@end
